/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigl.updater.modelupdater;

import java.util.HashMap;
import java.util.ArrayList;

/**
 *
 * @author uxmal
 */
public class PackageUpdater {

    HashMap packages;

    public PackageUpdater() {
        packages = new HashMap();
    }

    public PackageUpdater(HashMap packages) {
        this.packages = packages;
    }

    public boolean addToPackage(String packageName, String moduleName) {
        if (packageName == null) {
            return false;
        }
        HashMap packageData = null;
        ArrayList packageModules = null;
        if (packages.containsKey(packageName)) {
            packageData = (HashMap) packages.get(packageName);
            if (packageData.containsKey("modules")) {
                packageModules = (ArrayList) packageData.get("modules");
                if (!packageModules.contains(moduleName)) {
                    packageModules.add(moduleName);
                }
            } else {
                packageModules = new ArrayList();
                packageModules.add(moduleName);
                packageData.put("modules", packageModules);
            }
        } else { // il faut tout initialiser

            packageModules = new ArrayList();
            if (!packageModules.contains(moduleName)) {
                packageModules.add(moduleName);
            }
            packageData = new HashMap();
            packageData.put("modules", packageModules);
            packages.put(packageName, packageData);
        }
        return true;
    }

    public boolean hasPackage(String packageName) {
        return packages.containsKey(packageName);
    }

    public HashMap getPackages() {
        return packages;
    }

    public void setPackagePass(String packageName, String value) {
        if (packages.containsKey(packageName)) {
            HashMap packageData = (HashMap) packages.get(packageName);
            packageData.put("pass", value);
        }
    }

    public void setPackageFail(String packageName, String value) {
        if (packages.containsKey(packageName)) {
            HashMap packageData = (HashMap) packages.get(packageName);
            packageData.put("fail", value);
        }
    }

    public boolean hasBeenParsed(String packageName) {
        if (packages.containsKey(packageName)) {
            HashMap packageData = (HashMap) packages.get(packageName);
            if (packageData.containsKey("pass") && packageData.containsKey("fail")) {
                return true;
            }
        }
        return false;
    }

    public void setPackages(HashMap packages) {
        this.packages = packages;
    }

    public int size() {
        return packages.size();
    }
}
